package com.foreknow.web;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.User;
import com.foreknow.service.UserService;
import com.foreknow.utils.Constance;
import com.foreknow.utils.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Api(tags = "UserController", description = "用户管理管理")
@RestController
@RequestMapping("/user")
public class UserController{
    @Autowired
    private UserService userService;

    @ApiOperation("登录")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(@RequestBody Map<String, Object> para) throws JsonProcessingException {
        String username = (String) para.get("username");
        String password = (String) para.get("password");
        User user = userService.Login(username, password);
        HashMap<String, Object> hs = new HashMap<>();
        String token = null;
        if (user != null) {
            token = TokenUtil.sign(user);
            hs.put("meta", new Constance("msg", 200));
        } else {
            hs.put("meta", new Constance("msg", 403));
        }
        hs.put("token", token);
        hs.put("data", user);
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
}
